<?php
abstract class controller{
	public $path;
	public $link;
	public $self;
	public $database;
	public $url;
	public $auth;
	public $message;

	public function __construct($url, $auth=false){
		$this->path = "http://marketingman.hu/project/";
		$this->link = $this->path."index.php?url=";
		$this->self = implode("/", $url);
		$this->url = $url;
		$this->auth = $auth;
		$this->database = new database("localhost", "marketin_user", "Marketing123", "marketin_project");
		$this->message = null;
	}

	public function redirect($to, $message=null){
		if($message!=null) $to .= "&msg=".$message;
		header("Location: index.php?url=".$to);
	}

	public function start(){
		$this->check();
		
		/**/
		$this->logout();
		$this->auth();
		$this->setMessage();
		/**/

		$this->run();
		$this->startGui();
	}

	private function logout(){
		if(isset($_GET["logout"])) unset($_SESSION["user"]);
	}

	private function auth(){
		if($this->auth){
			if(!isset($_SESSION["user"]) || empty($_SESSION["user"])){
				$this->redirect("login","Az oldal megtekintéséhez be kell jelentkeznie!");
			}
		}
	}

	private function setMessage(){
		if(!empty($_GET["msg"])) $this->message = $_GET["msg"];
	}
	
	public abstract function check();

	public abstract function run();

	private function startGui(){
		$gui = new gui($this);
		$gui->show();
	}
}
?>
